//
//  XHStatusRetweetView.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/30.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
import SnapKit
class XHStatusRetweetView: UIView {
    var retweetViewBottomConstraint : Constraint?
    var status:XHStatus?{
        didSet{
            //给微博内容赋值
            contenLable.text = status?.retweeted_status?.text
            
            //卸载约束
            retweetViewBottomConstraint?.uninstall()
            
            //判断是否有转发微博视图
            if status?.retweeted_status?.pic_urls?.count > 0{
                //有视图
                //赋值
                
                pictureView.pic_urls = status?.retweeted_status?.pic_urls

                
                
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(Margin).constraint
                })
            }else{
                //没有视图
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(contenLable).offset(Margin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    private func setupUI(){
        pictureView.backgroundColor = UIColor.grayColor()
        addSubview(contenLable)
        addSubview(pictureView)
        contenLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(Margin)
            make.left.equalTo(self).offset(Margin)
        }
        //设置view自己的约束
        self.snp_makeConstraints { (make) -> Void in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(Margin).constraint
        }
        //添加转发微博图片
        pictureView.snp_makeConstraints { (make) -> Void in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contenLable.snp_bottom).offset(Margin)
            make.left.equalTo(contenLable)
        }
    }
    
    private lazy var contenLable:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWdith: SCREENW - 2*Margin)
    
    //转发微博的图 
    private lazy var pictureView:XHPictureView = XHPictureView()
    
}
